package Server.ServerCore.Controller;

import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Model.vo.CardVO;
import Server.ServerCore.Service.DataPacks.packRequest.Double_requst;
import Server.ServerCore.Service.DataPacks.packRespose.Chips_reback_Response;
import Server.ServerCore.Service.DataPacks.packRespose.Double_response;
import Server.ServerCore.Service.DataPacks.packRespose.bust_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;
import javolution.util.FastList;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;


/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-1
 *         Time: 下午9:57
 *         DoubleController=23 赌倍  注意modelVO 筹码set
 *         筹码不够，此包不会被发送
 *
 */
@Service
public class DoubleController extends HandleController {

    private static final long serialVersionUID = -4606083070207874978L;

    /**
     * 赌倍处理逻辑
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {
         Double_requst doubleRequst= (Double_requst) parseObject(buffer,Double_requst.class);

        int circleUnitID=doubleRequst.getCircleUnitID();

        //筹码变为下注前的两倍
        CardResultStatusVO cardResultStatusVO=player.getDeckoOfCardsVOFastList()[circleUnitID];
        cardResultStatusVO.setDouble((byte) 1);//赌倍flag
        double lastchips=player.getBargainingChip()-cardResultStatusVO.getChipsPoints();
        cardResultStatusVO.setChipsPoints(2*(cardResultStatusVO.getChipsPoints()));
        player.setBargainingChip(lastchips);
        PlayerManagerService.update(player.getGameRole(),lastchips);

        FastList<CardVO> playersCardVO=player.getCardVOFastList();

        //返回当前的筹码数
        Chips_reback_Response rebackResponse=new Chips_reback_Response();
        rebackResponse.setChips(lastchips);
        player.DirectSend(rebackResponse.getJsonData(2,7));

        //发牌 2,3
        CardVO cardVO=new CardVO();
        cardVO.setCircleUnitID(circleUnitID);
        playersCardVO.add(cardVO);


        Double_response doubleResponse=new Double_response();
        doubleResponse.setCircleUnitID(circleUnitID);
        doubleResponse.setCard(cardVO.getCardType().getID());
        doubleResponse.setCard_colour(cardVO.getCardcolour());
        player.DirectSend(doubleResponse.getJsonData(2,3));

        //指向下一门牌
        if (player.getNowCrileUnitID()==circleUnitID){//排除庄家的位置：-1
            bust_response bustResponse=new bust_response();
            int nextID=PlayerManagerService.getNextcircleUnitID(player, circleUnitID);
            bustResponse.setNextcircleUnitID(nextID);

            if (nextID!=-1){
                player.setNowCrileUnitID(nextID);
            }

            //计算玩家点数
            int points=PlayerManagerService.getPlayerPoints(player,circleUnitID);



            //最后一门加注没有跳到庄家
            if (points>=21&&nextID==-1){

                points=0;
            }else {

                player.DirectSend(bustResponse.getJsonData(2, 9));
            }
        }
    }
    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[23]=this;
    }
}
